﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Savico.Library.Commom.SqlServer
{
    public class Connection
    {
        public Connection()
        { }

        /// <summary>
        /// Get sql connection from web.config
        /// </summary>
        /// <returns>SqlConnection</returns>
        public SqlConnection ReadFromWebConfig()
        {
            SqlConnection conn = null;
            try
            {
                string conString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
                conn = new SqlConnection(conString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return conn;
        }

        /// <summary>
        /// Get sql connection from app.config
        /// </summary>
        /// <returns>SqlConnection</returns>
        public SqlConnection ReadFromAppConfig()
        {
            SqlConnection conn = null;
            try
            {
                string conString = "";
                conn = new SqlConnection(conString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return conn;
        }

        /// <summary>
        /// Get sql connection from connection string
        /// </summary>
        /// <param name="conString">connection string</param>
        /// <returns>SqlConnection</returns>
        public SqlConnection ReadFromString(string conString)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(conString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return conn;
        }

        /// <summary>
        /// Get sql connection from xml config file
        /// </summary>
        /// <param name="filename">xml file location</param>
        /// <returns>SqlConnection</returns>
        public SqlConnection ReadFromFile(string filename)
        {
            SqlConnection conn = null;
            try
            {
                string conString = "";
                //read from xml file and return connection string
                conn = new SqlConnection(conString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return conn;
        }
    }
}
